package edu.university.demo.system.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.smallchill.core.toolbox.file.BladeFileKit;

import edu.university.demo.common.AjaxResult;
import edu.university.demo.core.annotation.Permission;
import edu.university.demo.core.file.BladeFile;
import edu.university.demo.system.entit.Song;
import edu.university.demo.system.service.SongService;

@Controller
@RequestMapping("/system/song")
public class SongAdminController extends BaseController {
	
	@Autowired
	private SongService songService;

	@Permission("admin")
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String getSongList(Map<String, Object> map) {
		
		map.put("songList", songService.getAllSong());
		return "admin/song/songList";
	}
	
	/**
	 * 删除歌曲
	 */
	@Permission("admin")
	@RequestMapping("/delete/{id}")
	public AjaxResult deleteSong(@PathVariable("id") Long id) {
		if (songService.deleteSong(id)) {
			return success();
		}
		return error();
	}
	
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String addSong() {
		
		return "admin/song/addSong";
	}

	
	/**
	 * 添加新歌
	 */
	@Permission("admin")
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResult newSong(Song song) {
		if (songService.newSong(song)) {
			
			return success();
		}
		return error();
	}

	/**
	 * 
	 * 通过文件名截取歌手,歌名,
	 * 把歌手的拼音作为图片的文件名,
	 * 并返回前台回显
	 */
	@ResponseBody
	@RequestMapping("/convert")
	public Map<String, String> convertSong(String fileName) {
		
		Map<String, String> songMap = Maps.newHashMap();
		if(fileName.indexOf("-")!=-1) {
			fileName = fileName.replaceAll(" ", "");
			String singer = fileName.substring(0, fileName.indexOf("-"));
			String songName = 
					fileName.substring(fileName.indexOf("-")+1, fileName.indexOf("."));
			songMap.put("songName", songName);
			songMap.put("singer", singer);
		}
		return songMap;
	}

	/**
	 * 上传音乐文件
	 * @throws Exception 
	 */
	@Permission("admin")
	@ResponseBody
	@RequestMapping("/upload/mp3")
	public AjaxResult uploadMp3(MultipartFile songFile){
		
		if(!songFile.isEmpty()) {
			String originalFileName = songFile.getOriginalFilename();
			boolean ok = BladeFileKit.testExt("media", originalFileName);
			if(!ok) {
				return error("不支持该文件类型");
			}
		}else {
			return error("请选择要上传的歌曲");
		}
		BladeFile bladeFile = BladeFile.getFile(songFile, "/data/media");
		bladeFile.transfer();
		
		return success();
	}
	/**
	 * 上传图片
	 * */
	@Permission("admin")
	@ResponseBody
	@RequestMapping("/upload/img")
	public AjaxResult uploadIMG(MultipartFile imgFile){
		
		if(!imgFile.isEmpty() ) {
			String originalFileName = imgFile.getOriginalFilename();
			boolean ok = BladeFileKit.testExt("image", originalFileName);
			if(!ok) {
				return error("不支持该图片类型");
			}
		}else{
			return error("请选择要上传的图片");
		}
		BladeFile bladeFile = BladeFile.getFile(imgFile, "/data/image");
		bladeFile.transfer();
		
		return success();
	}
	
	/**
	 * 上传歌词
	 * */
	@Permission("admin")
	@ResponseBody
	@RequestMapping("/upload/lyr")
	public AjaxResult uploadLyr(MultipartFile lyrFile){
		
		if(!lyrFile.isEmpty()) {
			String originalFileName = lyrFile.getOriginalFilename();
			String extName = BladeFileKit.getFileExt(originalFileName);
			boolean ok = ".lrc".equals(extName);
			if(!ok) {
				return error("只能选择lrc格式的歌词");
			}
		}else{
			return error("请选择要上传的歌词文件");
		}
		BladeFile bladeFile = BladeFile.getFile(lyrFile, "/data/lyric");
		bladeFile.transfer();
		
		return success();
	}
	
	@ExceptionHandler(Exception.class)
	public Object exceptionHandler(Exception ex, HttpServletResponse response, HttpServletRequest request) {
		//ex.printStackTrace();
		if (ex instanceof MaxUploadSizeExceededException) {
			long maxSize = ((MaxUploadSizeExceededException)ex).getMaxUploadSize();
			return error("文件不能超过" +maxSize / 1024 / 1024 + "m");
		}
		return null;
	}
}
